using System;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Text;
namespace time
{
    public class person4
    {
        public string name { get; set; }
        public string age { get; set; }
    }

    public class work
    {
        public static void Main()
        {
            person4 p1 = new person4() { name = "1", age = "1" };
            person4 p2 = new person4() { name = "2", age = "2" };
            string s = json(p1);
            Console.WriteLine(s);
            write_file(s);
            Console.Read();
        }

        public static string json<T>(T t)
        {
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
            MemoryStream ms = new MemoryStream();
            ser.WriteObject(ms, t);
            string jsonString = Encoding.UTF8.GetString(ms.ToArray());
            ms.Close();
            return jsonString;

        }

        public static void write_file(string content)
        {
            string s = "1.txt";
            StreamWriter sw = new StreamWriter(s, false);
            sw.Write(content);
            sw.Flush();
            sw.Close();
        }
    }
}

